<template>
  <Dialog
    v-model="dialogVisible"
    width="800px"
    :title="t('market.industryNews.globalPrompts')"
    :before-close="() => closeDialog(formRef)"
  >
    <el-form :model="form" ref="formRef" :rules="rules as FormRules" label-width="90px">
      <el-form-item :label="t('market.industryNews.promptWords')" prop="cue_word">
        <el-input
          v-model="form.cue_word"
          type="textarea"
          :autosize="{ minRows: 15, maxRows: 20 }"
          :placeholder="t('market.industryNews.promptWordsMsg')"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button class="m-r-4" @click="closeDialog(formRef)"
        >{{ t('system.parameterConfig.cancel') }}
      </el-button>
      <el-button type="primary" @click="submit(formRef)"
        >{{ t('market.industryNews.save') }}
      </el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { Dialog } from '@/components/Dialog'
import { useI18n } from '@/hooks/web/useI18n'
import { type FormInstance, type FormRules, ElMessage } from 'element-plus'
import { getGlobalPromptsApi, saveGlobalPromptsConfigApi } from '@/api/market'

const emit = defineEmits(['refresh'])
const { t } = useI18n()

let dialogVisible = ref<boolean>(false)

const formRef = ref<FormInstance>()
const form = ref<Record<string, any>>({
  cue_word: ''
})

const rules = ref<FormRules>({
  cue_word: [
    {
      required: true,
      message: t('market.industryNews.promptWordsMsg'),
      trigger: 'change'
    }
  ]
})

const submit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      const resp = await saveGlobalPromptsConfigApi(form.value)
      if (resp.code === 0) {
        ElMessage.success(t('market.industryNews.statusMsg'))
        closeDialog(formEl)
        emit('refresh')
      }
    }
  })
}

const closeDialog = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  form.value = {
    cue_word: ''
  }
  formEl.resetFields()
  dialogVisible.value = false
}

const getPlatformSelectList = async () => {
  // const resp = await getGlobalPromptsApi()
  // form.value.cue_word = resp.data.cue_word
}

const getBackShow = () => {
  getPlatformSelectList()
}

defineExpose({
  dialogVisible,
  getBackShow
})
</script>
<style lang="less" scoped></style>
